/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controller;

import Model.CreditCard;
import Model.Customer;
import Model.CustomerManager;
import Model.DeliveryAddress;
import Model.RandomGenerator;
import Model.User;
import Model.UserManager;
import Model.UserSecure;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author 013v
 */
public class CustomerRegister extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {

                User newUser = new User();
                newUser.setUsername(request.getParameter("username"));
                newUser.setEmail(request.getParameter("email"));

                boolean checkUser = new UserManager().checkUserExists(request.getParameter("username"));
                boolean checkEmail = new UserManager().checkEmailExists(request.getParameter("email"));
                
                if (checkUser == false && checkEmail == false) {
                    int newID = new RandomGenerator().generateAccountID();
                
                    newUser.setAccountID(Integer.toString(newID));
                    newUser.setPassword(request.getParameter("password"));
                    newUser.setFirstName(request.getParameter("fname"));
                    newUser.setMiddleInitial(request.getParameter("minitial"));
                    newUser.setLastName(request.getParameter("sname"));
                    newUser.setUserType(6);
                    System.out.println("user");
                    CreditCard newCred = new CreditCard();
                    newCred.setCCfirstName(request.getParameter("fcardname"));
                    newCred.setCClastName(request.getParameter("scardname"));
                    newCred.setCCCardNum(request.getParameter("cardNumber"));
                    newCred.setCCType(request.getParameter("cardType"));
                    newCred.setCCExpiry(request.getParameter("expiry"));
                    newCred.setCCBHouseNum(request.getParameter("Bhouseno"));
                    newCred.setCCBStreet(request.getParameter("Bst"));
                    newCred.setCCBSubdivision(request.getParameter("Bsubd"));
                    newCred.setCCBCity(request.getParameter("Bcity"));
                    newCred.setCCBZip(request.getParameter("Bzipcode"));
                    newCred.setCCBCountry(request.getParameter("Bcountry"));
                    newCred.setAccountID(Integer.toString(newID));
                   
                    DeliveryAddress newDeliv = new DeliveryAddress();
                    newDeliv.setDAHouseNum(request.getParameter("Dhouseno"));
                    newDeliv.setDAStreet(request.getParameter("Dst"));
                    newDeliv.setDASubdivision(request.getParameter("Dsubd"));
                    newDeliv.setDACity(request.getParameter("Dcity"));
                    newDeliv.setDAZip(request.getParameter("Dzipcode"));
                    newDeliv.setDACountry(request.getParameter("Dcountry"));
                    newDeliv.setAccountID(Integer.toString(newID));

                    UserSecure newSec = new UserSecure();
                    newSec.setSecurityQuestion(request.getParameter("secQ"));
                    newSec.setSecurityAnswer(request.getParameter("secA"));
                    newSec.setAccountID(Integer.toString(newID));

                    Customer newCust = new Customer();
                    newCust.setNewUser(newUser);
                    newCust.setNewCred(newCred);
                    newCust.setNewDeliv(newDeliv);
                    newCust.setNewSec(newSec);


                    boolean result = new CustomerManager().addCustomer(newCust);
                    
                    if (result == true) {
                        response.sendRedirect("index.jsp?register=true");
                    }

                }else {
                    response.sendRedirect("signUp.jsp?istaken=true");
                }
          

            }  finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
